* load data from:
* 		WHO Global Database on Child Growth and Malnutrition
*			Children aged <5 years stunted
*			https://platform.who.int/nutrition/malnutrition-database/database-search

cd "~/Documents/econ/research/current/cognitive/estimation/data"
import delimited using GHO_stunted.csv, clear
keep if sex=="BTSX"
rename numeric stunted
rename country countrycode
keep countrycode year stunted datasource
sort countrycode year

// choose source where there are duplicates
drop if countrycode=="BGD" & year=="2012-2013"
drop if countrycode=="BGD" & year=="2004" & datasource=="NLIS_312705"
drop if countrycode=="BGD" & year=="1999-2000"
drop if countrycode=="BGD" & year=="1996-1997"
drop if countrycode=="BGD" & year=="1995-1996"
drop if countrycode=="BGD" & year=="1992" & datasource=="NLIS_311568"
drop if countrycode=="LBY" & year=="1995" & datasource=="NLIS_312858"
drop if countrycode=="PAK" & year=="1990-1994" & datasource=="NLIS_311931"
drop if countrycode=="RWA" & year=="2000" & datasource=="NLIS_313075"
replace year = "2013" if countrycode=="SEN" & year=="2012-2013"
drop if countrycode=="SLE" & year=="2010" & datasource=="NLIS_313149"
drop if countrycode=="SDN" & year=="2006" & datasource=="NLIS_312911"
drop if countrycode=="TGO" & year=="1988" & datasource=="NLIS_310439"
drop if countrycode=="USA" & year=="1999-2004" & datasource=="NLIS_312859"
drop if countrycode=="VNM" & year=="2006" & datasource!="NLIS_312857"
drop if countrycode=="VNM" & (year=="1999" | year=="2000") & datasource!="NLIS_312951"

rename year yearstr
gen int yearbeg = real(substr(yearstr,1,4))
gen int yearend = real(substr(yearstr,-4,4))
gen int year = floor((yearbeg+yearend)/2)
// l countrycode year yearstr if year!=yearbeg
drop yearstr yearbeg yearend datasource
label var stunted "% of children under age 5 stunted"
sort countrycode year
saveold stunted, replace version(11)

// by countrycode: gen lastyear = (_n==_N)

encode countrycode, generate(isonum)
xtset isonum year
xtset
tsfill
drop countrycode
decode isonum, generate(countrycode)
by isonum: ipolate stunted year, gen(stunt)
drop stunted isonum
label var stunt "% of children under age 5 stunted"

preserve
keep if year>=1990 & year<=1995
sort countrycode year
by countrycode: keep if _n==1
tab year
sort countrycode
replace year = 1990
saveold stunt1990, replace version(11)

restore, preserve
keep if year>=2000 & year<=2005
sort countrycode year
by countrycode: keep if _n==1
tab year
sort countrycode
replace year = 2000
saveold stunt2000, replace version(11)

restore
keep if year>=2005 & year<=2010
sort countrycode year
by countrycode: keep if _n==_N
tab year
sort countrycode
replace year = 2010
saveold stunt2010, replace version(11)
